﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlServerCe;
using System.Collections;

namespace CPTECH.Repository
{
    class PlantaDB: BaseDB
    {
        private static PlantaDB instance;

        private PlantaDB() { }

        public static PlantaDB getInstance()
        {
            if (instance == null)
            {
                instance = new PlantaDB();
            }
            return instance;
        }

        //retorna todos os locais na base
        public ArrayList selectAllPlantas()
        {
            try
            {
                this.open();
                SqlCeCommand command = new SqlCeCommand("select cd_fis_planta, ds_planta from TB_PLANTA;", this.conn);
                SqlCeDataReader rdr = command.ExecuteReader();
                ArrayList result = new ArrayList();

                while (rdr.Read())
                {
                    result.Add(rdr.GetInt64(0) + " = " + rdr.GetString(1));
                }

                return result;
            }
            finally
            {
                this.close();
            }

        }

        //checa se código filial já existe.
        public bool checkCodPlanta(Int64 codPlanta)
        {
            bool result = false;
            try
            {
                this.open();
                SqlCeCommand command = new SqlCeCommand("select * from TB_PLANTA where cd_fis_planta =@codPlanta;", this.conn);
                command.Parameters.AddWithValue("@codPlanta", codPlanta);
                SqlCeDataReader rdr = command.ExecuteReader();
                while (rdr.Read())
                {
                    result = true;
                }
            }
            finally
            {
                this.close();
            }
            return result;
        }

        //inseri novo Local.
        public bool insertNewPlanta(string os, Int64 codigoFilial, Int64 codLocal, Int64 codPlanta, string descPlanta, string dtRegistro, byte enviado)
        {
            bool resposta = false;
            try
            {
                this.open();
                SqlCeCommand command = new SqlCeCommand("insert into TB_PLANTA values('" + os + "','" + codigoFilial + "', '"+ codLocal + "','" + codPlanta + "','" + descPlanta +
                                                        "','" + dtRegistro + "','" + enviado + "');", this.conn);
                command.ExecuteNonQuery();
                resposta = true;
            }
            catch (FormatException)
            {
                resposta = false;
            }
            finally
            {
                this.close();
            }
            return resposta;
        }
    }
}
